﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using DataAccess;
namespace BusinessLogic
{
   public static class BCXuatHuyHangHoa
    {
      static Entities.ChiTietXuatHuy ctxh;
      static Entities.CongTy congty;
        public static Entities.PhieuXuatHuy[] SelectPXHTheoTrangThai()
        {
            Entities.PhieuXuatHuy[] list = null;
            try
            {
                string sql = "sp_SelectPXHTheoTrangThai";
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.PhieuXuatHuy phieuxuathuy = new Entities.PhieuXuatHuy();
                    phieuxuathuy.PhieuXuatHuyID = int.Parse(dr[Common.Constants.PhieuXuatHuy.PhieuXuatHuyID].ToString());
                    phieuxuathuy.MaPhieuXuatHuy = dr[Common.Constants.PhieuXuatHuy.MaPhieuXuatHuy].ToString();
                    phieuxuathuy.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuXuatHuy.NgayLap].ToString());
                    phieuxuathuy.MaNhanVien = dr[Common.Constants.PhieuXuatHuy.MaNhanVien].ToString();
                    phieuxuathuy.TenNhanVien = (dr[Common.Constants.PhieuXuatHuy.TenNhanVien].ToString());
                    phieuxuathuy.MaKho = dr[Common.Constants.PhieuXuatHuy.MaKho].ToString();
                    phieuxuathuy.TenKho = (dr[Common.Constants.PhieuXuatHuy.TenKho].ToString());
                    phieuxuathuy.TrangThai = bool.Parse(dr[Common.Constants.PhieuXuatHuy.TrangThai].ToString());
                    phieuxuathuy.Tongtien = (dr[Common.Constants.PhieuXuatHuy.Tongtien].ToString());
                    phieuxuathuy.GhiChu = dr[Common.Constants.PhieuXuatHuy.GhiChu].ToString();
                    phieuxuathuy.Deleted = bool.Parse(dr[Common.Constants.PhieuXuatHuy.Deleted].ToString());
                    arr.Add(phieuxuathuy);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.PhieuXuatHuy[0];
                }
                else
                {
                    list = new Entities.PhieuXuatHuy[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.PhieuXuatHuy)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.PhieuXuatHuy[0]; }

        }
        public static Entities.BCXuatHuyHangHoa Select()
        {
            Entities.BCXuatHuyHangHoa banghi = new Entities.BCXuatHuyHangHoa();
            try
            {
                banghi.Khohang = BusinessLogic.KhoHang.SelectAllKhoHang();
                banghi.Phieuxuathuy = SelectPXHTheoTrangThai();
                banghi.Chitietxuathuy = BusinessLogic.ChiTietXuatHuy.Select1(ctxh);
                banghi.Congty = BusinessLogic.CongTy.Select(congty);
            }
            catch (Exception ex)
            { string s = ex.Message; }
            return banghi;
        }
    }
}
